package com.ruanyun.modular.app.controller;

import com.aliyuncs.exceptions.ClientException;
import com.ruanyun.modular.admin.service.TMeasuresService;
import com.ruanyun.modular.app.model.params.TUserParam;
import com.ruanyun.modular.app.service.TUserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.ruanyun.util.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 用户表控制器
 *
 * @author zs
 * @Date 2021-04-20 15:35:31
 */
@Controller
@RequestMapping("/app/user")
@CrossOrigin
public class TUserController extends BaseController {


    @Autowired
    private TUserService tUserService;

    @Autowired
    private TMeasuresService measuresService;

    /**
     * 注册
     * @param phone
     * @param pwd
     * @param code
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public ResponseData register( @RequestParam("phone") String phone,
                                 @RequestParam("pwd") String pwd,
                                  @RequestParam("code") String code,
                                 @RequestParam("socialCode") String socialCode,
                                  @RequestParam("pid") String pid){
        return tUserService.register(phone,pwd,code,socialCode,pid);
    }


    /**
     * 获取手机验证码
     * @param phone
     * @param type
     * @return
     * @throws IOException
     * @throws ClientException
     */
    @RequestMapping("/getCode")
    @ResponseBody
    public ResponseData getCode( @RequestParam("phone") String phone,
                                 @RequestParam("type") String type) throws IOException, ClientException {
        return tUserService.getCode(phone,type);
    }



    /**
     * 登录
     * @param phone
     * @param pwd
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseData login(HttpServletRequest request, HttpServletResponse response,
                              @RequestParam("phone") String phone,
                              @RequestParam("pwd") String pwd){
        return tUserService.login(phone,pwd);
    }

    /**
     * 修改密码
     * @param request
     * @param phone
     * @param code
     * @return
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public ResponseData updatePwd(HttpServletRequest request,
                                  @RequestParam("phone") String phone,
                                   @RequestParam("code") String code,
                                   @RequestParam("newPwd") String newPwd) {
        //获取token中的userId
        String userId = new TokenUtils().getUserId(request);
        return tUserService.updatePwd(userId,phone,code,newPwd);
    }


    /**
     * 忘记密码
     * @param phone
     * @param code
     * @param newPwd
     * @return
     */
    @RequestMapping("/forgetPwd")
    @ResponseBody
    public ResponseData forgetPwd( @RequestParam("phone") String phone,
                                   @RequestParam("code") String code,
                                   @RequestParam("newPwd") String newPwd) {
        return tUserService.forgetPwd(phone,code,newPwd);
    }


    /**
     * 换绑手机号
     * @param request
     * @param oldPhone
     * @param newPhone
     * @param code
     * @return
     */
    @RequestMapping("/bindingPhone")
    @ResponseBody
    public ResponseData bindingPhone(HttpServletRequest request,
                                      @RequestParam("oldPhone") String oldPhone,
                                      @RequestParam("newPhone") String newPhone,
                                      @RequestParam("code") String code) {
        //获取token中的userId
        String userId = new TokenUtils().getUserId(request);
        return tUserService.bindingPhone(userId,oldPhone,newPhone,code);
    }


    /**
     * 修改用户信息
     * @param request
     * @param userParam
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
    public ResponseData updateUser(HttpServletRequest request, @RequestBody TUserParam userParam) {
        //获取token中的userId
        String userId = new TokenUtils().getUserId(request);
        userParam.setId(userId);
        return tUserService.updateUser(userParam);
    }


    /*获取提交信息*/
    /**

     * @param
     * @param
     * @return
     */
    @RequestMapping("/getCommitBasic")
    @ResponseBody
    public ResponseData getCommit(HttpServletRequest request){
        String userId = new TokenUtils().getUserId(request);

        return ResponseData.success( tUserService.getCommit(userId));
    }

    /*获取其他分页信息*/
    /**

     * @param
     * @param
     * @return
     */
    @RequestMapping("/getCommitOther")
    @ResponseBody
    public ResponseData getCommitOther(HttpServletRequest request,Integer type,Integer emissionWay,Integer planType){
        String userId = new TokenUtils().getUserId(request);

        return  tUserService.getCommitOther(userId,type,emissionWay, planType);
    }



    /*获取vocs综合减排措施*/
    /**

     * @param
     * @param
     * @return
     */
    @RequestMapping("/getVocsRedPlan")
    @ResponseBody
    public ResponseData getVocsRedPlan(HttpServletRequest request,Integer redType,Integer measureType){
        String userId = new TokenUtils().getUserId(request);

        return  tUserService.getVocsRedPlan(userId,redType,measureType);
    }


    /*获取有组织排放信息*/
     @RequestMapping("getOrganizedInfo")
     @ResponseBody
     public ResponseData getOrganizedInfo(Integer monitoringId,Integer isOrganization){
         return  tUserService.getOrganizedInfo(monitoringId,isOrganization);
     }



    /*获取有组织排放污染物信息*/
    @RequestMapping("getOrganizedPollutantsInfo")
    @ResponseBody
    public ResponseData getOrganizedPollutantsInfo(Integer monitoringId,Integer isOrganization,Integer orificeId){
        return  tUserService.getOrganizedPollutantsInfo(monitoringId,isOrganization,orificeId);
    }



    /*获取厂内位置信息*/
    @RequestMapping("getUnorganizedLocationInfo")
    @ResponseBody
    public ResponseData getUnorganizedInfo(Integer monitoringId,Integer isOrganization){
        return  tUserService.getUnorganizedLocationInfo(monitoringId,isOrganization);
    }

    /*获取厂内污染物信息*/
    @RequestMapping("getInsidePollutantsInfo")
    @ResponseBody
    public ResponseData getInsidePollutantsInfo(Integer monitoringId,Integer isOrganization,Integer id){
        return  tUserService.getInsidePollutantsInfo(monitoringId,isOrganization,id);
    }


    /*获取厂界污染物信息*/
    @RequestMapping("getUnInsidePollutantsInfo")
    @ResponseBody
    public ResponseData getUnInsidePollutantsInfo(Integer monitoringId,Integer isOrganization,Integer windType){
        return  tUserService.getUnInsidePollutantsInfo(monitoringId,isOrganization,windType);
    }

    @RequestMapping("getCountyList")
    @ResponseBody
    public ResponseData getCountyList(){

        return  ResponseData.success(tUserService.getCountyList());
    }


    /*获取减排合计*/
    @RequestMapping("getMeasureEm")
    @ResponseBody
    public ResponseData getMeasureEm(Integer companyId){
        return ResponseData.success(measuresService.measureHeadInfo(companyId));
    }


}


